﻿

using Hammock.Authentication.OAuth;
using RedBit.UserVoice.Model;
using Newtonsoft.Json.Linq;
using System;
namespace RedBit.UserVoice.Services
{
    public abstract class BaseUVApi
    {


        protected static OAuthCredentials BuildCredentials()
        {
            var credentials = new OAuthCredentials
            {
                Type = OAuthType.ProtectedResource,
                SignatureMethod = OAuthSignatureMethod.HmacSha1,
                ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
                ConsumerKey = UserVoiceAPI.Key,
                ConsumerSecret = UserVoiceAPI.Secret,
            };
            return credentials;
        }

        protected static UVError ParseError(JObject response)
        {

            if (response["errors"] != null && response["errors"].HasValues)
            {
                try
                {
                    return new UVError()
                    {
                        Type = response["errors"]["type"].Value<string>(),
                        Message = response["errors"]["message"] != null ? response["errors"]["message"].Value<string>() : "",
                        User = response["errors"]["user"] != null ? response["errors"]["user"].Value<string>() : "",
                    };
                }
                catch(Exception ex)
                {
                    //TODO log the exception?
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    return new UVError() { Message = "unknown", Type = "unknown" };
                }
            }
            return null;
        }

    }
}
